//
//  SquareSprite.m
//  Assignment1Q3Q4
//
//  Created by LxL on 2012-10-17.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "SquareSprite.h"


@implementation SquareSprite
@synthesize  contentNode=_contentNode;

-(id) init
{
    // set of different sizes= {16,64,256}
    int randomsize= pow((arc4random_uniform(3)+2)*2,2);
    
    self.contentNode=nil;

    if( (self=[super initWithFile:@"whitesquare.png" rect:CGRectMake(0, 0, randomsize, randomsize)]) )
    {
        int minSpeed=2;
        int maxSpeed=4;
        int range=maxSpeed-minSpeed;
        
        int velX=arc4random_uniform(range*2)-minSpeed;
        int velY=arc4random_uniform(range*2)-minSpeed;
        
        if(velX==0){
            velX=2;
        }
        if(velY==0){
            velY=2;
        }
        
        velocity= ccp(velX,velY);
        
        winSize= [[CCDirector sharedDirector] winSize];
        
        int minX= self.contentSize.height/2;
        int minY=minX;
        int maxX=winSize.width-self.contentSize.height/2;
        int maxY=winSize.height-self.contentSize.height/2;
        
        int rangeX=maxX-minX;
        int rangeY=maxY-minY;
        self.position= ccp(arc4random_uniform(rangeX)+minX,arc4random_uniform(rangeY)+minY);
    }
    return self;
}
-(void) update:(ccTime) dt
{
    CGPoint tmp=ccp(self.position.x+velocity.x,self.position.y+velocity.y);
    
    
    if(tmp.x>=winSize.width-self.contentSize.width/2||tmp.x<=self.contentSize.width/2)
    {
        velocity.x= -velocity.x;
    }
    if(tmp.y>=winSize.height-self.contentSize.width/2||tmp.y<=self.contentSize.width/2)
    {
        velocity.y= -velocity.y;
    }
    //tmp=ccp(self.position.x+velocity.x,self.position.y+velocity.y);
    self.position=ccp(tmp.x,tmp.y);
    
}

-(void) checkCollideWith:(SquareSprite*) other
{
    int actualDX= fabsf(self.position.x-other.position.x);
    int actualDY= fabsf(self.position.y-other.position.y);
    
    float width= (self.contentSize.width+other.contentSize.width)*0.5f;
    float height= (self.contentSize.height+other.contentSize.height)*0.5f;
    
    if(actualDX<width&&actualDY<height)
    {
        [self setColor:ccRED];
        [self setOpacity:150];
        [other setColor:ccRED];
        [other setOpacity:150];
    }
    
//    if(CGRectIntersectsRect(self.boundingBox,other.boundingBox)){
//        [self setColor:ccRED];
//        [other setColor:ccRED];
//    }
}



@end
